package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.FlowRecord;
import ysq.xyg.model.Member;
import ysq.xyg.model.info.FlowRecordInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.FlowRecordService;
import ysq.xyg.service.MemberService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * 流量记录
 *
 * @author congcong tao
 * @date 2018/9/5 13:52
 */
@Controller
@RequestMapping("/flowRecord")
public class FlowRecordController extends BaseController {
    
    private static Logger log= LoggerFactory.getLogger(FlowRecordController.class);
    @Resource
    private FlowRecordService flowRecordService;
    @Resource
    private MemberService memberService;
    
    private FlowRecord flowRecord;

    @RequestMapping("/listPage")
    public ModelAndView list(){
        return new ModelAndView("/flowRecordList");
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(FlowRecord flowRecord, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<FlowRecord> pageInfo = flowRecordService.pageInfo(flowRecord, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (FlowRecord item : pageInfo.getList()) {
                row=new JSONObject();
                row.put("id", item.getId());
                row.put("activityTitle", item.getActivityTitle());
                row.put("itemTitle", item.getItemTitle());
                row.put("memberName", item.getMemberName());
                row.put("flowCount", item.getFlowCount());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id){
        ModelAndView view = new ModelAndView("/flowRecordUpdateOrSave");
        flowRecord = flowRecordService.selectByPrimaryKey(id);
        view.addObject("flowRecord", flowRecord);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(FlowRecordInfo flowRecordInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(flowRecordInfo.getId())) {
            flowRecord = new FlowRecord();
            flowRecord.setId(BaseUtil.uId());
            flowRecord.setActivityId(flowRecordInfo.getActivityId());
            flowRecord.setFlowId(flowRecordInfo.getFlowId());
            flowRecord.setFlowCount(flowRecordInfo.getFlowCount());
            flowRecord.setMemberId(flowRecordInfo.getMemberId());
            flowRecord.setCreateTime(new Date());
            int id = flowRecordService.insert(flowRecord);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
            // 减去本身有的流量
            Member member = memberService.selectByPrimaryKey(flowRecordInfo.getMemberId());
            int flowNum = member.getFlowNum();
            String flowCount = flowRecordInfo.getFlowCount();
            member.setFlowNum(flowNum - Integer.parseInt(flowCount));
            memberService.updateByPrimaryKeySelective(member);
        } else {
            flowRecord = flowRecordService.selectByPrimaryKey(flowRecordInfo.getId());
            int id = flowRecordService.updateByPrimaryKey(flowRecord);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if (StringUtil.isEmpty(id)) {
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        } else {
            int ids = flowRecordService.deleteByPrimaryKey(id);
            if (ids <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
